<?php
	header("Content-Type: text/html; charset=iso-8859-1");

	session_start();
	if(!$_SESSION['logged']){
		header("Location: index.php");
		exit;
	}

    require_once('include/db_connection.php');
    require_once('include/script_includes.php');
    require_once('include/config.inc.php');
	//require_once('dartautomat.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>MMS-Dart-Liga</title>
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type"/>
        <link rel="stylesheet" type="text/css" href="css/global.css"/>
        <link rel="stylesheet" type="text/css" href="css/jquery.jqplot.min.css"/>
        <link rel="stylesheet" type="text/css" href="css/jquery-ui.css"/>
		<link rel="stylesheet" type="text/css" href="css/jquery.contextMenu.css"/>
		<link rel="stylesheet" type="text/css" href="dist/themes/default/style.min.css" />
		<link rel="stylesheet" type="text/css" href="css/statistics.css"/>

        <script type="text/javascript" src="scripts/jquery-2.1.0.min.js"></script>
        <script type="text/javascript" src="scripts/jquery-ui.js"></script>
		<script type="text/javascript" src="scripts/jquery.ui.position.js"></script>
		<script type="text/javascript" src="scripts/jquery.contextMenu.js"></script>
		<script type="text/javascript" src="scripts/jquery.windowmsg-1.0.js"></script>
		<script type="text/javascript" src="scripts/jquery.hoverIntent.minified.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jquery.jqplot.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.highlighter.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.cursor.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.dateAxisRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.canvasTextRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.canvasAxisTickRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.categoryAxisRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.BezierCurveRenderer.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.trendline.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.dragable.min.js"></script>
        <script type="text/javascript" src="scripts/jqplot/jqplot.pieRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.ohlcRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.barRenderer.min.js"></script>
		<script type="text/javascript" src="scripts/jqplot/jqplot.pointLabels.min.js"></script>
		<script type="text/javascript" src="scripts/jquery.iosslider.js"></script>

    </head>
    <body>
        <div id="statisticsMain"></div>
    </body>
	
	<script type="text/javascript">
		jQuery(window).resize(function() {
			jQuery('#statisticsMain').width(jQuery(window).width());
		});
	
		jQuery(document).ready(function() {	
			var playerArray = <?php echo(json_encode($_GET['playerArray'])); ?>;
			jQuery('#gameSave .gridtable tr td.statisticByPlayer:lt(10)').each(function(item) {console.log($(this).data('player-id'))})
			
			$.each(playerArray, function( index, playerID ) {
				// build surrounding container				
				jQuery('#statisticsMain').append("<div id='PLAYER_"+playerID+"' class='playerContainer'></div>");
				jQuery('#statisticsMain').width(jQuery(window).width());
				var playerCont = $('#PLAYER_'+playerID);
				var playerName = getPlayerName(playerID);
				console.log(playerName);
				var targetID = "statistics_1_"+playerID;
				jQuery(playerCont).append("<div id='"+targetID+"' class='playerHits'></div>");
				console.log(playerCont);
				var url = "ajax_statistics.php?request=statisticHitsPlayer&playerID="+playerID;
				jQuery.ajax({
					type:       "POST",
					url: 		url,
					dataType:   'html',
					success:    function(data){
						var resultObj = eval (data);
						var ticks = resultObj[0];
				 
						plot1 = $.jqplot(targetID, [resultObj[1]], {
							title: playerName,
							seriesDefaults:{
								renderer:$.jqplot.BarRenderer,
								rendererOptions: { barWidth: '20' },
								pointLabels: { show: true }
							},
							axes: {
								xaxis: {
									renderer: $.jqplot.CategoryAxisRenderer,
									ticks: ticks
								},
								yaxis: {
									renderer: $.jqplot.CategoryTickRenderer,
									min:0,
									max:parseInt(resultObj[1][0])+parseInt(resultObj[1][0])/5,
									tickOptions: {
									  showGridline: false,
									  showMark: false,
									  showLabel: false,
									  shadow: false,
									},
								},
							}
						});
						plot1.replot( );
					}
				});
				
				var targetID2 = "statistics_2_"+playerID;
				jQuery(playerCont).append("<div id='"+targetID2+"' class='playerHits'></div>");
				var url = "ajax_statistics.php?request=statisticHitsPlayerPie&playerID="+playerID;
				jQuery.ajax({
					type:       "POST",
					url: 		url,
					dataType:   'html',
					success:    function(data){
						var resultObj = eval (data);
						var plot1 = jQuery.jqplot (targetID2, [resultObj], 
						{ 
							seriesDefaults: {
								renderer: jQuery.jqplot.PieRenderer, 
								rendererOptions: {
								  showDataLabels: true
								}	
							}, 
							legend: { show:true, location: 'e', placement:'insideGrid'}
						});
						plot1.replot( );
					}
				});
				
				var targetID3 = "statistics_3_"+playerID;
				jQuery(playerCont).append("<div id='"+targetID3+"' class='playerHits'></div>");
				var url = "ajax_statistics.php?request=statisticWeeksPlayer&playerID="+playerID;
				jQuery.ajax({
					type:       "POST",
					url: 		url,
					dataType:   'html',
					success:    function(data){
						var resultObj = eval (data);
						plot1 = $.jqplot(targetID3,[resultObj],{
							seriesDefaults:{yaxis:'y2axis'},
							axes: {
								  xaxis: {
									renderer: $.jqplot.DateAxisRenderer,
									tickOptions: { formatString: '%d.%m.%y' }
								},
								y2axis: {
									min:0,
									max:1,
									ticks:[0, 0.25, 0.5, 0.75, 1],
									tickOptions:{ prefix: '' }
								}
							  },
							  series: [{renderer:$.jqplot.OHLCRenderer, 
										rendererOptions:{	candleStick:true,
															upBodyColor:'#33CC00',fillUpBody:true,
															downBodyColor:'#FF0000',fillDownBody:true}}
							  ],
							  cursor:{ 
								show: true,
								zoom:true, 
								showTooltip:true
							 } 
						});
					}
				});
			});
		});
		
		function getPlayerName(playerID) {
			var url = "ajax_statistics.php?request=getNameForID&playerID="+playerID;
			return $.ajax({
				type:       "POST",
				url: 		url,
				dataType:   'text',
				async:      false
			}).responseText;
		}		
	</script>
</html>